﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace _DataTable转List
{
    class Program
    {
        static void Main(string[] args)
        {
            //创建DataTable 
            DataTable dt = new DataTable("Table");
            //创建Id列 自增 主键
            dt.Columns.Add("Id", typeof(int));
            //设置自增列 
            dt.Columns["Id"].AutoIncrement = true;
            //默认值1开始
            dt.Columns["Id"].AutoIncrementSeed = 1;
            //设置主键
            DataColumn[] Key = new DataColumn[1];
            Key[0] = dt.Columns["Id"];
            dt.PrimaryKey = Key;
            //创建其他列
            dt.Columns.Add("Name", typeof(string));
            dt.Columns.Add("Sex", typeof(string));
            dt.Columns.Add("Birthday", typeof(DateTime));
            dt.Columns.Add("InputTime", typeof(DateTime));

            //创建列值
            DataRow dr = dt.NewRow();
            dr["Name"] = "张三";
            dr["Sex"] = "男";
            dr["Birthday"] = DateTime.Now.AddYears(-18);
            dr["InputTime"] = DateTime.Now;
            //给DataTable添加列值
            dt.Rows.Add(dr);

            DataRow dr1 = dt.NewRow();
            dr1["Name"] = "小红";
            dr1["Sex"] = "女";
            dr1["Birthday"] = DateTime.Now.AddYears(-19);
            dr1["InputTime"] = DateTime.Now;
            //给DataTable添加列值
            dt.Rows.Add(dr1);

            List<Person> ps = GetList<Person>(dt);

            List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
            //List<Entity> rows = new List<Entity>(); //EntityList
            ///然后dt转list
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                //Entity e = new Entity();
                Dictionary<string, object> row = new Dictionary<string, object>();
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    row.Add(dt.Columns[j].ColumnName, dt.Rows[i][j]);
                    //e.Proty = dt.Rows[i][dt.Columns[j].ColumnName];
                }
                rows.Add(row);
            }

            Console.ReadKey();


        }

        /// <summary>
        /// 通用list转Model
        /// </summary>
        /// <typeparam name="T">泛型实体类</typeparam>
        /// <param name="dt">DataTable数据</param>
        /// <returns>List<T></returns>
        public static List<T> GetList<T>(DataTable dt) where T : new()
        {
            PropertyInfo[] Info = typeof(T).GetProperties();
            List<T> list = new List<T>();
            foreach (DataRow dr in dt.Rows)
            {
                T t = new T();
                foreach (DataColumn col in dt.Columns)
                {
                    foreach (PropertyInfo info in Info)
                    {
                        if (info.Name == col.ColumnName)
                        {
                            info.SetValue(t, dr[col.ColumnName]);
                        }
                    }
                }
                list.Add(t);
            }
            return list;
        }

        public class Person
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public string Sex { get; set; }
            public DateTime Birthday { get; set; }
            public DateTime InputTime { get; set; }
        }
    }
}
